<!-- 排行 -->
<template>
  <SearchTable :query="data.query" :list="data.list" :total="data.total" @changeSize="changeSize"
    @changePage="changePage" @search="getSearchList" @reset="onHideDetails(true)" @changeTh="data.dialogVisible = true">
    <template #other>
      <el-tabs v-model="data.query.order" type="card" class="demo-tabs" @tab-click="handleClick">
        <el-tab-pane :label="t('623')" :name="1" />
        <el-tab-pane :label="t('624')" :name="2" />
        <el-tab-pane :label="t('44')" :name="3" />
        <el-tab-pane :label="t('625')" :name="4" />
      </el-tabs>
    </template>
    <template #form>
      <GameItem ref="GameItemRef" @query="queryGame" />
      <el-form-item :label="$t('39')">
        <el-date-picker v-model="data.query.date" type="daterange" range-separator="-" :start-placeholder="$t('26')"
          :end-placeholder="$t('27')" value-format="YYYY-MM-DD" :disabled-date="indexStore.disabledDateTime"/>
      </el-form-item>
    </template>
    <template #button></template>
    <template #table>
      <template v-for="(item, index) in data.fields" :key="index">
        <el-table-column
          v-if="item.label == '下注金额' && data.query.order == 3 ? true : item.label == '胜率' && data.query.order == 4 ? true : item.visible"
          :prop="item.prop" :label="item.label" :width="item.width">
          <template #header>
            <el-tooltip v-if="item.tips" effect="dark" placement="top">
              <template #content>
                {{ item.tips }}
              </template>
              <el-icon class="ques-tips">
                <QuestionFilled />
              </el-icon>
            </el-tooltip>
            {{ item.label }}
          </template>
          <template v-if="item.prop == 'account'" #default="scope">
            <span class="item_details" @click="data.isUserDetails=true, data.checkedItem = scope.row">
              {{ scope.row.account }}
            </span>
          </template>
          <template v-if="item.prop == 'game_id'" #default="scope">
            {{ GameName(indexStore.gameArray, scope.row.game_id) }}
          </template>
          <template v-if="item.prop == 'sum_bet'" #default="scope">
            <span class="colorText">{{ scope.row.sum_bet }}</span>
          </template>
          <template v-if="item.prop == 'win_lose'" #default="scope">
            <span :class="colorClass(scope.row.win_lose)">{{
              scope.row.win_lose
            }}</span>
          </template>
          <template v-if="item.prop == 'history_win_lose'" #default="scope">
            <span :class="colorClass(scope.row.history_win_lose)">{{
              scope.row.history_win_lose
            }}</span>
          </template>
          <template v-if="item.prop == 'wallet'" #default="scope">
            <span class="colorText">{{ scope.row.wallet }}</span>
          </template>
        </el-table-column>
      </template>
    </template>
  </SearchTable>

  <!-- 变更表头弹框 -->
  <tableHeader :dialogVisible="data.dialogVisible" :list="data.fields" @cancel="data.dialogVisible = false"
    @resetData="resetData" />
  <!-- 玩家信息详情 -->
  <UserDetails :show="data.isUserDetails" @onHideDetails="onHideDetails" :checkedItem="data.checkedItem" />
</template>

<script lang="ts" setup>
import { ref, reactive, toRefs, onMounted, computed } from 'vue'
import type { TabsPaneContext } from 'element-plus'
import GameItem, { type GameType } from '@/components/FormItem/GameItem.vue'
import tableHeader from '@/components/tableHeader/tableHeader.vue'
import UserDetails from '@/views/users/components/user-details.vue'
import SearchTable from '@/components/SearchTable/SearchTable.vue'
import { getRankList } from '@/api/report'
import { GameName, colorClass } from '@/utils/index'
import { BigPercent, BigWan } from '@/utils/math'
import { getDateDay, setTimeValue, setTimestampTime } from '@/utils/time'

import { useI18n } from 'vue-i18n'
const { t } = useI18n()
import { useStore } from '@/stores/index'
const indexStore = useStore()

export interface commercialType {
  value: number
  label: string
}
const data = reactive({
  query: {
    page: 1,
    size: 100,
    order: 1,
    game_id: -1,
    platform: -1,
    date: [getDateDay(0), getDateDay(0)]
  },
  list: [],
  total: 0,
  fields: [
    { label: t('627'), prop: 'index', width: '90', visible: true, tips: t('627') },
    { label: t('18'), prop: 'account', width: '180', visible: true, tips: t('290') },
    { label: t('44'), prop: 'sum_bet', width: '', visible: false, tips: t('615') },
    { label: t('628'), prop: 'win_ratio', width: '', visible: false, tips: t('629') },
    { label: t('377'), prop: 'win_lose', width: '', visible: true, tips: t('537') },
    { label: t('378'), prop: 'history_win_lose', width: '', visible: true, tips: t('538') },
    { label: t('31'), prop: 'wallet', width: '', visible: true, tips: t('305') },
    { label: t('630'), prop: 'game_count', width: '', visible: true, tips: t('616') },
    { label: t('24'), prop: 'created_time_stamp', width: '180', visible: true, tips: t('299') },
    { label: t('32'), prop: 'last_login_time', width: '180', visible: true, tips: t('298') },
  ],
  dialogVisible: false,
  exportLoading: false,
  isUserDetails: false,
  checkedItem: {} as any
})

// 游戏品牌 + 游戏
const queryGame = (item: GameType) => {
  data.query.platform = item.platform
  data.query.game_id = item.game_id
}

onMounted(async () => {
  getSearchList()
})

// 选择游戏tab
const handleClick = async (tab: TabsPaneContext) => {
  data.query.order = tab.paneName as number
  getSearchList()
}

const getSearchList = async () => {
  data.query = setTimeValue(data.query)
  let res = await getRankList(data.query)
  res.data?.forEach((item: any, index: number) => {
    item.index = index + 1
    item.created_time_stamp = setTimestampTime(item.created_time_stamp)
    item.last_login_time = setTimestampTime(item.last_login_time)
    item.sum_bet = BigWan(item.sum_bet)
    item.win_lose = BigWan(item.win_lose)
    item.history_win_lose = BigWan(item.history_win_lose)
    item.wallet = BigWan(item.wallet)
    item.win_ratio = BigPercent(item.win_times, item.bet_times) // 胜率 =  中奖次数 / 总下注次数
  })
  data.list = res.data
}

// 修改页数
const changeSize = (val: number) => {
  data.query.size = val
}

const changePage = (val: number) => {
  data.query.page = val
  getSearchList()
}

// 重置表头
const resetData = (val: any) => {
  data.fields = val
}

// 重置
const GameItemRef = ref<any>()
const onHideDetails = (show: boolean) => {
  data.isUserDetails = false
  data.checkedItem = {} as any
  if (show) {
    data.query = {
      page: 1,
      size: 50,
      order: data.query.order,
      game_id: -1,
      platform: -1,
      date: [getDateDay(0), getDateDay(0)]
    }
    GameItemRef.value.onHideDetails()
    // getOperatedList()
  }
}
</script>

<style scoped lang="scss"></style>